package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.conmon.MyPageData;
import com.woniuxy.conmon.enums.RedisConst;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.pojo.Advertisement;
import com.woniuxy.pojo.dto.AdvertisementDto;
import com.woniuxy.pojo.dto.SaveAdvertisementDto;
import com.woniuxy.service.AdvertisementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/advertisement")
public class AdvertisementController {
    @Autowired
    private AdvertisementService advertisementService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/getAdListData/{pageNum}/{pageSize}")
    public ResponseEntity<?> getAdListData(@PathVariable Integer pageNum,
                                           @PathVariable Integer pageSize,
                                           @RequestBody AdvertisementDto dto){
        Page<Advertisement> page = advertisementService.getAdListData(pageNum, pageSize,dto);
        MyPageData data = new MyPageData(page.getTotal(),page.getRecords());
        return ResponseEntity.ok(data);
    }

    @GetMapping("/getAdPositions")
    public ResponseEntity<?> getAdPositions(){
        LambdaQueryWrapper<Advertisement> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Advertisement::getPosition);
        List<Advertisement> list = advertisementService.list(wrapper);
        return ResponseEntity.ok(list);
    }
    @DeleteMapping("/batchDelete")
    public ResponseEntity<?> batchDelete(@RequestBody List<Integer> ids){
        boolean flag = advertisementService.removeByIds(ids);
        return ResponseEntity.ok(flag? SystemCode.SUCCESS.getMessage(): SystemCode.FAIL.getMessage());
    }
    @PostMapping("/batchPublish")
    public ResponseEntity<?> batchPublish(@RequestBody List<Integer> ids){
        if (ids==null || ids.isEmpty()){
            return ResponseEntity.ok(SystemCode.FAIL.getMessage());
        }
        LambdaUpdateWrapper<Advertisement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Advertisement::getAdId,ids).set(Advertisement::getStatus,1);
        boolean flag = advertisementService.update(wrapper);
        return ResponseEntity.ok(flag?SystemCode.PUBLISH_SUCCESS.getMessage(): SystemCode.FAIL.getMessage());
    }
    @PostMapping("/batchOffline")
    public ResponseEntity<?> batchOffline(@RequestBody List<Integer> ids){
        if (ids==null || ids.isEmpty()){
            return ResponseEntity.ok(SystemCode.FAIL.getMessage());
        }
        LambdaUpdateWrapper<Advertisement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Advertisement::getAdId,ids).set(Advertisement::getStatus,0);
        boolean flag = advertisementService.update(wrapper);
        return ResponseEntity.ok(flag?SystemCode.PUBLISH_SUCCESS.getMessage(): SystemCode.FAIL.getMessage());
    }
    @PutMapping("/changeStatus/{id}/{status}")
    public ResponseEntity<?> changeStatus(@PathVariable Integer id,@PathVariable Integer status){
        LambdaUpdateWrapper<Advertisement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Advertisement::getAdId,id).set(Advertisement::getStatus,status);
        advertisementService.update(wrapper);
        return ResponseEntity.ok(SystemCode.SUCCESS.getMessage());
    }
    @PutMapping("/top/{id}")
    public ResponseEntity<?> top(@PathVariable Integer id){
        LambdaUpdateWrapper<Advertisement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Advertisement::getAdId,id).set(Advertisement::getTopLevel,1);
        advertisementService.update(wrapper);
        return ResponseEntity.ok(SystemCode.STICKY_SUCCESS.getMessage());
    }
    @PutMapping("/unpin/{id}")
    public ResponseEntity<?> unpin(@PathVariable Integer id){
        LambdaUpdateWrapper<Advertisement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Advertisement::getAdId,id).set(Advertisement::getTopLevel,0);
        advertisementService.update(wrapper);
        return ResponseEntity.ok(SystemCode.CANCEL_STICKY_SUCCESS.getMessage());
    }
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable Integer id){
        boolean flag = advertisementService.removeById(id);
        return ResponseEntity.ok(flag?SystemCode.SUCCESS.getMessage(): SystemCode.FAIL.getMessage());
    }
    @PostMapping("/saveOrUpdateAdvertisement")
    public ResponseEntity<?> saveOrUpdateAdvertisement(@RequestBody SaveAdvertisementDto dto){
        Advertisement advertisement = new Advertisement();
        BeanUtils.copyProperties(dto,advertisement);
        advertisement.setStatus(0);
        advertisement.setTopLevel(0);
        Boolean flag =advertisementService.saveOrUpdate(advertisement);
        if (flag){
            redisTemplate.opsForSet().add(RedisConst.UPLOAD_IMAGES_DB,advertisement.getImageUrl());
        }
        return ResponseEntity.ok(SystemCode.SUCCESS.getMessage());
    }
    @GetMapping("/getAdvertisementById/{adId}")
    public ResponseEntity<?> getAdvertisementById(@PathVariable Integer adId){
        LambdaQueryWrapper<Advertisement> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Advertisement::getAdId,adId);
        Advertisement advertisement = advertisementService.getOne(wrapper);
        return ResponseEntity.ok(advertisement);
    }
}
